package com.zlm.user.emp.service.impl;

import com.zlm.common.base.AbstractBaseService;
import com.zlm.common.entity.AreaDO;
import com.zlm.common.entity.BasAreaDO;
import com.zlm.common.entity.CityDO;
import com.zlm.common.entity.ProvinceDO;
import com.zlm.user.emp.dao.BasAreaDao;
import com.zlm.user.emp.service.BasAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: duyufu
 * @Date: 2019/4/29 9:45
 */
@Service
public class BasAreaServiceImpl extends AbstractBaseService<BasAreaDao, BasAreaDO> implements BasAreaService {
    @Autowired
    @Override
    public void setBaseDao(BasAreaDao basAreaDao) {
        this.baseDao = basAreaDao;
    }

    @Override
    public List<ProvinceDO> queryAllCity() {
        List<BasAreaDO> listArea= baseDao.queryAllCity();
        AreaDO area = new AreaDO();
        List<AreaDO> areaList = new ArrayList<>();
        CityDO city = new CityDO();
        List<CityDO> cityList = new ArrayList<>();
        ProvinceDO province = new ProvinceDO();
        List<ProvinceDO> result = new ArrayList<>();
        Integer tempProvinceId = null;
        Integer tempCityId = null;
        for (BasAreaDO v:listArea ) {
            if (v.getParentId() == -1) {
                if (province.getAreaCode() != null) {
                    if (city.getAreaCode() != null) {
                        if (areaList.size() > 0) {
                            city.setChildren(areaList);
                            areaList = new ArrayList<>();
                        }
                        cityList.add(city);
                        city = new CityDO();
                    }
                    if (cityList.size() > 0) {
                        province.setChildren(cityList);
                        cityList = new ArrayList<>();
                    }
                    result.add(province);
                }
                province = new ProvinceDO();
                province.setAreaCode(v.getAreaCode());
                province.setAreaName(v.getAreaName());
                tempProvinceId = v.getAreaId();
            } else if (tempProvinceId != null &&
                    v.getParentId().intValue() == tempProvinceId.intValue()){
                if (city.getAreaCode() != null) {
                    if (areaList.size() > 0) {
                        city.setChildren(areaList);
                        areaList = new ArrayList<>();
                    }
                    cityList.add(city);
                    city = new CityDO();
                }
                city.setAreaCode(v.getAreaCode());
                city.setAreaName(v.getAreaName());
                tempCityId = v.getAreaId();
            } else if (tempCityId != null &&
                    v.getParentId().intValue() == tempCityId.intValue()) {
                area = new AreaDO();
                area.setAreaCode(v.getAreaCode());
                area.setAreaName(v.getAreaName());
                areaList.add(area);
            }
        }
        if (province.getAreaCode() != null) {
            if (city.getAreaCode() != null) {
                if (areaList.size() > 0) {
                    city.setChildren(areaList);
                }

                cityList.add(city);
            }
            if (cityList.size() > 0) {
                province.setChildren(cityList);
            }
            result.add(province);
        }
        return result;
    }

    @Override
    public List<Map<String, Object>> queryPrefectureLevelCity() {
        List<BasAreaDO> listArea= baseDao.queryPrefectureLevelCity();
        List<Map<String,Object>> menu=new ArrayList<>();
        for (BasAreaDO basAreaDO : listArea) {
            Map<String, Object> regionMap = new HashMap<String, Object>();
            String name = basAreaDO.getAreaName();
            Integer code = Integer.parseInt(basAreaDO.getAreaCode());
            String firstLetter = basAreaDO.getFirstLetter();
            regionMap.put("name", name);
            regionMap.put("code", code);
            regionMap.put("firstLetter", firstLetter);
            menu.add(regionMap);
        }
        return menu;
    }
}
